﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace RemoveUnusedCSSRule
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("必须得是两个参数");
            }
            else
            {
                string sourceFile = args[0];
                string patternFile = args[1];

                string sourceText = File.ReadAllText(sourceFile);
                string[] patternLines = File.ReadAllLines(patternFile);

                foreach (string patternLine in patternLines)
                {
                    string patternLineWithoutChar = patternLine
                        .Replace(",", "\\,")
                        .Replace("=", "\\=")
                        .Replace("[", "\\[")
                        .Replace(".", "\\.")
                        .Replace("]", "\\]")
                        .Replace(":", "\\:")
                        .Replace("\"", "\\\"")
                        .Replace("-", "\\-")
                        .Replace("'", "\\'");

                    string pattern = @"^"+ patternLineWithoutChar + @"[\s\n]+\{([^\}]|[\r\n])+\}[\s\n]+";
                    sourceText = Regex.Replace(sourceText, pattern, string.Empty, RegexOptions.IgnoreCase | RegexOptions.Multiline);

                    Console.WriteLine(pattern);
                }

                string fileName = sourceFile;

                File.WriteAllText(string.Format("{0}\\{1}.{2}{3}", Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName), "compiled", Path.GetExtension(fileName)), sourceText);

//                Console.WriteLine(string.Format("{0}\\{1}.{2}{3}", Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName), "compiled", Path.GetExtension(fileName)), sourceText);
            }

        }
    }
}
